package org.game.gobang.client.support;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import org.game.component.AlertBox;
import org.game.gobang.client.constant.ClientConstant;
import org.game.result.RestResult;
import org.game.result.RestResultBuilder;
import org.game.result.ResultCode;
import org.game.util.JsonUtil;

public class JoinRoomBox extends AlertBox {
    public JoinRoomBox(String title) {
        super(title);
        display();
    }

    @Override
    protected Scene slot() {
        GridPane gridPane = new GridPane();

        Button join = new Button("加入");
        join.setOnAction(event -> {
            String roomName = getTitle().split(" | ")[0];

            RestResult restResult = new RestResultBuilder().success(ResultCode.JOIN_ROOM);
            restResult.setData(roomName);

            ClientConstant.ctx.writeAndFlush(JsonUtil.toJson(restResult));
            ClientConstant.refreshMap();

            close();
        });
        gridPane.add(join, 0, 0);
        Button cancel = new Button("取消");
        cancel.setOnAction(event -> {
            close();
        });
        gridPane.add(cancel, 1, 0);

        Scene scene = new Scene(gridPane);
        return scene;
    }
}
